import config from '@/config';
import $XTools from '@/utils/x-tools';
import { $XTrack } from '@/report';

const navigate = function(item, params) {
  const linkType = item.linkType;
  let linkContent = item.linkContent;
  let linkUrl = '';
  let { sourcePageType, sourcePageId, source, sourceFrom, groupsSource } = params;
  if (!params.sourcePageType) {
    sourcePageType = '';
  }
  if (!params.sourcePageId) {
    sourcePageId = '';
  }
  if (!params.source) {
    source = '';
  }
  if (!params.sourceFrom) {
    sourceFrom = '';
  }
  if (!params.groupsSource) {
    groupsSource = '';
  }

  // "linkType": 1, //链接类型： 1-微页面；2-商品链接；3-全部商品；4-分类；5-排行榜；6-源头工厂；7-99精选；8-活动；9-消息；10-我的；11-领券中心；12-活动补贴；13-买就送；14-小程序路径；15-公众号路径；16-投放页
  switch (linkType) {
    case 1: {
      const params = {
        pageId: linkContent
      };
      if (sourcePageType) {
        params.pageType = sourcePageType;
      }
      if (sourcePageId) {
        params.sourcePageId = sourcePageId;
      }
      if (source) {
        params.source = source;
      }
      const query = $XTools.object2param(params);
      linkUrl = `/pages/decorate/index?${query}`;
      break;
    }
    case 2: {
      const params = {
        productId: linkContent
      };
      if (sourcePageType) {
        params.sourcePageType = sourcePageType;
      }
      if (sourcePageId) {
        params.sourcePageId = sourcePageId;
      }
      if (source) {
        params.source = source;
      }
      if (sourceFrom) {
        params.sourceFrom = sourceFrom;
      }
      const query = $XTools.object2param(params);
      linkUrl = `/pages/transit-product/index?${query}`;
      break;
    }
    case 3:
      linkUrl = '/packages/activity/all-goods/index';
      break;
    case 4:
      linkUrl = '/packages/sort/index';
      break;
    case 5:
      linkUrl = '/packages/ranking/index/index';
      break;
    case 6:
      linkUrl = '/packages/activity/source-factory/index';
      break;
    case 8:
      linkUrl = '/packages/activity/index/index';
      break;
    case 10:
      linkUrl = '/pages/my/index';
      break;
    case 11:
      linkUrl = '/packages/coupon/coupon-centre/index';
      break;
    case 12:
      linkUrl = `/packages/activity/subsidy/list?componentId=${linkContent}`;
      break;
    case 14:
      if (linkContent.indexOf('/', 0) !== 1) {
        linkContent = '/' + linkContent;
      }
      linkUrl = linkContent;
      break;
    case 15:
      linkUrl = `/packages/activity/flower/index?webViewUrl=${encodeURIComponent(linkContent)}`;
      break;
    case 16: {
      const params = {
        pageId: linkContent
      };
      if (sourcePageType) {
        params.pageType = sourcePageType;
      }
      if (sourcePageId) {
        params.sourcePageId = sourcePageId;
      }
      if (source) {
        params.source = source;
      }
      if (sourceFrom) {
        params.sourceFrom = sourceFrom;
      }
      if (groupsSource) {
        params.groupsSource = groupsSource;
      }
      const query = $XTools.object2param(params);
      // if (item.pageType === 2) {
      //   linkUrl = `/pages/immersive/index?${query}`;
      // } else {
        linkUrl = `/pages/advertising/index?${query}`;
      // }
      break;
    }
    case 23:
      linkUrl = '/pages/index/index';
      break;
  }
  const switchUrl = [
    '/pages/index/index',
    '/pages/order/index',
    '/pages/my/index'
  ];
  const alternateNavigate = function(item, sourcePageType, sourcePageId, source) {
    // 跳转失败逻辑
    const newItem = item;
    if (newItem?.isAlternateLink) {
      return;
    }
    newItem.linkType = item?.alternateLinkType;
    newItem.linkContent = item?.alternateLinkContent;
    newItem.isAlternateLink = true;
    navigate(newItem, sourcePageType, sourcePageId, source);
  };
  if (switchUrl.indexOf(linkUrl) > -1) {
    uni.switchTab({
      url: linkUrl
    });
  } else if (linkType === 18) {
    // 18-视频号主页;19-视频号视频;20-视频号活动页;21-视频号直播;22-预约视频号直播;
    // 跳转视频号主页
    wx.openChannelsUserProfile({
      finderUserName: config.finderUserName,
      success(res) {
        console.log(res);
      }
    });
  } else if (linkType === 19) {
    // 跳转视频号视频
    wx.openChannelsActivity({
      finderUserName: config.finderUserName,
      feedId: item.linkContent,
      success(res) {
        console.log(res);
      },
      fail(err) {
        // 跳转失败逻辑
        console.log(err);
        $XTrack('onAlternateErrorEvent', err);
        if (err.errMsg !== 'openChannelsActivity:fail cancel') {
          alternateNavigate(item, sourcePageType, sourcePageId, source);
        }
      }
    });
  } else if (linkType === 20) {
    // 跳转视频号活动页
    wx.openChannelsEvent({
      finderUserName: config.finderUserName,
      eventId: item.linkContent,
      success(res) {
        console.log(res);
      },
      fail(err) {
        // 跳转失败逻辑
        console.log(err);
        $XTrack('onAlternateErrorEvent', err);
        if (err.errMsg !== 'openChannelsActivity:fail cancel') {
          alternateNavigate(item, sourcePageType, sourcePageId, source);
        }
      }
    });
  } else if (linkType === 21) {
    wx.getChannelsLiveInfo({
      finderUserName: config.finderUserName,
      success(res) {
        console.log(res);
        // 如果能获取到直播间信息，并且在直播
        if (res.errMsg.indexOf('ok') && res.status === 2) {
          wx.openChannelsLive({
            finderUserName: config.finderUserName,
            feedId: res.feedId,
            nonceId: res.nonceId,
            success(success) {
              console.log(success);
            },
            fail(err) {
              console.log(err);
              $XTrack('onAlternateErrorEvent', err);
              if (err.errMsg !== 'openChannelsActivity:fail cancel') {
                alternateNavigate(item, sourcePageType, sourcePageId, source);
              }
            }
          });
        } else {
          if (!item?.alternateLinkType) {
            wx.openChannelsLive({
              finderUserName: config.finderUserName,
              feedId: res.feedId,
              nonceId: res.nonceId,
              success(success) {
                console.log(success);
              },
              fail(err) {
                console.log(err);
                $XTrack('onAlternateErrorEvent', err);
                if (err.errMsg !== 'openChannelsActivity:fail cancel') {
                  alternateNavigate(item, sourcePageType, sourcePageId, source);
                }
              }
            });
          } else {
            // 跳转失败逻辑
            alternateNavigate(item, sourcePageType, sourcePageId, source);
          }
        }
      },
      fail(err) {
        console.log(err);
        // 跳转失败逻辑
        $XTrack('onAlternateErrorEvent', err);
        if (err.errMsg !== 'openChannelsActivity:fail cancel') {
          alternateNavigate(item, sourcePageType, sourcePageId, source);
        }
      }
    });
  } else if (linkType === 22) {
    // 预告 id，通过 getChannelsLiveNoticeInfo 接口获取
    wx.getChannelsLiveNoticeInfo({
      finderUserName: config.finderUserName,
      success(res) {
        console.log(res);
        // 预约视频号直播
        wx.reserveChannelsLive({
          noticeId: res.noticeId
        });
      },
      fail(err) {
        console.log(err);
        // 跳转失败逻辑
        $XTrack('onAlternateErrorEvent', err);
        if (err.errMsg !== 'openChannelsActivity:fail cancel') {
          alternateNavigate(item, sourcePageType, sourcePageId, source);
        }
      }
    });
  } else {
    uni.navigateTo({
      url: linkUrl
    });
  }
};
export default navigate;
